﻿using HtmlAgilityPack;
using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class YanVn
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public System.Windows.Forms.HtmlDocument GetHtmlAjax(Uri uri, int AjaxTimeLoadTimeOut)
        {
            using (WebBrowser wb = new WebBrowser())
            {
                wb.Navigate(uri);
                while (wb.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
                Thread.Sleep(AjaxTimeLoadTimeOut);
                Application.DoEvents();
                return wb.Document;
            }
        }
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder) 
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

          
            string urlGoc = "http://www.yan.vn";

            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(urlGoc);
         //  var item2= GetHtmlAjax(urlGoc, 5);
            var listUrlCategory = doc.DocumentNode.SelectNodes("//div[@class='dvBannerMenu']//div//a");
            int dem = 0;
            for (int j = 0; j < 3; j++)
            {
                string itemPage = urlGoc+ listUrlCategory[j].Attributes[0].Value;
               ListCate.AddRange(load.LoadCatesWithSub(itemPage, "//div[@class='dvZoneCeleb']//div[@class='dvCelebBg']", "//div[@class='dvVideoCategoryTitle dvSmallTitle17']", urlGoc, manuId,"","//a","//a",null,false));

                ListCate.AddRange(load.LoadCatesWithSub(itemPage, "//div[@class='dvBannerMenu']//div[@class='dvIconGVideo']", "//div[@class='dvMain']", urlGoc, manuId,"//div[@class='dvVideoCategoryTitle dvSmallTitle17']","//a","//a",null,false));



             


                
                for (int i = 0; i < ListCate.Count(); i++)
                {
                    //    ListPage = load.LoadPage(ListCate[i], "//span[@id='ctl00_leftContent_ctl01_pager']//a", urlGoc);


                 

                   var ListDetail = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='dvVideoAllFrame']//div[@class='dvVideoFrameCategory']",
                        "//div[@class='dvVideoItemText']//a","//div[@class='dvCelebItemTeaser']//span", "//div[@class='dvVideoItemText']//a", urlGoc);



                 

                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = load.LoadNews(ListDetail[k], "//div[contains(@class,'dvCategoryBoxContent')]//span[@id='ctl00_MainContent_lbContent']//p", "", manuId, 6,"//span[@id='ctl00_MainContent_lbContent']//img",urlGoc,false);
                        if (item != null)
                        {
                            dem++;
                            load.AddItemToListView(item);
                            pdb.SaveImgNormal(folder, item);
                           

                        }
                    
                        //else
                        //{
                        //    var itemVideo = load.LoadNews(ListDetail[k], "//div[@class='dvVideoPageReadPanel']", "", manuId, 0,"","",0,"");
                        //    if (itemVideo != null)
                        //    {
                        //        dem++;
                        //        load.AddItemToListView(itemVideo);
                        //    }
                           

                        //}


                    }

                   

                }


            }
          



     //       System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }

    }
}
